package com.wedu.dao.impl;

import com.wedu.dao.StudentDao;
import com.wedu.entity.Student;
import com.wedu.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/*
   Spring框架对JDBC的简单封装。提供了一个JdbcTemplate对象简化JDBC开发
   步骤：
        1.导入jar包
        2.创建JdbcTemplate对象，依赖于数据源DataSource
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        3.调用JdbcTemplate的方法来完成CRUD的操作
            update():执行DML语句的增删改操作
            queryForMap():将查询结果封装为map集合，列名作为key，对应的值作为value(只能查询一条记录)
            queryForList():将每一个查询记录封装为map集合，再将map放进List集合中(可以查询多条记录)
            query():将结果封装为JavaBean对象
                query的参数为RowMapper
                *一般我们使用BeanPropertyRowMapper实现类。可以完成数据到JavaBean对象的自动封装
                *new BeanPropertyRowMapper<JavaBean类名>(JavaBean类名.class)
            queryForObject():查询结果，将结果封装为对象，一般用于聚合函数的查询

 */
public class StudentDaoImpl implements StudentDao {
    //获取JDBCTemplate对象
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    //根据id查找学生
    @Override
    public Student findOneById(int id) {
        //定义sql
        try {
            String sql="select * from student where id = ?";
            Student student = template.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class),id);
            return student;
        } catch (DataAccessException e) {
            return null;
        }
    }

    //登录判断
    @Override
    public Student login(String name, String password) {
        //定义sql
        try {
            String sql="select * from student where name = ? and password = ?";
            Student student = template.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), name, password);
            /*
                RowMapper和BeanPropertyRowMapper的区别：前者需要自己写一个匿名内部类，后者可以实现数据到JavaBean类的自动封装
             */
            return student;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int register(String name,String sex,int age,String password) {
        String sql="insert into student(name,age,sex,password) values (?,?,?,?)";
        return template.update(sql, name, age, sex, password);
    }

    @Override
    public Student updateMsg(int id,String name,int age,String sex,String password) {
        Student student = findOneById(id);

        if (student!=null){
            String sql="update student set `name`=?,age=?,sex=?,password=? where id=?";
            template.update(sql,name,age,sex,password,id);
        }
        return findOneById(id);
    }

    @Override
    public Student deleteStu(int id) {
        Student student = findOneById(id);

        if (student!=null) {
            String sql = "DELETE FROM `db1`.`student` WHERE `id` = ?";
            int i = template.update(sql, id);
            System.out.println(i);
        }else {
            return null;
        }
        return student;
    }
}
